package cas.ihep.fs.file;

import cas.ihep.fs.UnifiedBlock;
import cas.ihep.fs.UnifiedFile;
import cas.ihep.fs.UnifiedFileSystem;

import java.io.*;
import java.net.URI;

class LocalFile implements UnifiedFile,UnifiedBlock{

    File file;
    private URI path;
    LocalFile(URI uri){
        file=new File(uri);
        path=uri.normalize();
    }

    void create(UnifiedFileSystem.CreateOptions options){
        if(options.createParent()) {
            //noinspection ResultOfMethodCallIgnored
            file.getParentFile().mkdirs();
        }
    }

    InputStream openStream()throws IOException{
        return new FileInputStream(file);
    }

    OutputStream createStream() throws FileNotFoundException {
        return new FileOutputStream(file);
    }

    @Override
    public String absolutePath() throws IOException {
        return file.getCanonicalPath();
    }

    @Override
    public long id() {
        return 0;
    }

    @Override
    public long size() {
        return file.length();
    }

    @Override
    public void cancel() {

    }

    @Override
    public int permission() {
        return PERMISSION_RWX;
    }

    @Override
    public long length() {
        return file.length();
    }

    @Override
    public URI path() {
        return path;
    }

    @Override
    public UnifiedBlock block(int index) {
        return this;
    }

    @Override
    public UnifiedBlock next() {
        return this;
    }

    @Override
    public void delete() {
        //noinspection ResultOfMethodCallIgnored
        file.delete();
    }

    @Override
    public void persist() {
    }

    @Override
    public void free() {
    }

    @Override
    public void close() {

    }
}
